#-*- coding: utf-8 -*
#@author 小测试
#@create 2020-03-12 23:20
"""
读取配置文件
"""
from configparser import ConfigParser
from ApiDemo.util.get_log import MyLog
from ApiDemo.util.get_path import get_root_path
import os

class Config:
    # titles:
    TITLE_TEST = "test"
    TITLE_EMAIL = "mail"
    TITLE_CASE="case"

    # values:
    # [debug\release]
    VALUE_TESTER = "tester"
    VALUE_ENVIRONMENT = "environment"
    VALUE_VERSION_CODE = "versionCode"
    VALUE_HOST = "host"
    VALUE_LOGIN_HOST = "loginHost"
    VALUE_LOGIN_Data = "loginData"
    # [mail]
    VALUE_SMTP_SERVER = "smtpserver"
    VALUE_SENDER = "sender"
    VALUE_RECEIVER = "receiver"
    VALUE_USERNAME = "username"
    VALUE_PASSWORD = "password"

    #case
    VALUE_CASEID="测试编号"
    VALUE_CASENAME="接口名字"
    VALUE_CASERANK="用例等级"
    VALUE_CASEDESC="用例描述"
    VALUE_CASEDATA="请求数据"
    VALUE_CASEEXPECT="预期结果"
    # path

    def __init__(self):
        """
        初始化
        """
        self.log=MyLog()
        self.config = ConfigParser()
        self.conf_path = get_root_path()+"/pytest.ini"
        self.log.info("读取配置文件pytest.ini--->{}".format(self.conf_path))

        if not os.path.exists(self.conf_path):
            raise FileNotFoundError("请确保配置文件存在！")

        self.config.read(self.conf_path, encoding='gbk')
        """
        测试环境配置
        """
        self.tester_test = self.get_conf(Config.TITLE_TEST, Config.VALUE_TESTER)
        self.environment_test = self.get_conf(Config.TITLE_TEST, Config.VALUE_ENVIRONMENT)
        self.versionCode_test = self.get_conf(Config.TITLE_TEST, Config.VALUE_VERSION_CODE)
        self.host_test = self.get_conf(Config.TITLE_TEST, Config.VALUE_HOST)
        self.loginHost_test = self.get_conf(Config.TITLE_TEST, Config.VALUE_LOGIN_HOST)
        self.loginData_test = self.get_conf(Config.TITLE_TEST, Config.VALUE_LOGIN_Data)

        """
        邮件配置
        """
        self.smtpserver = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_SMTP_SERVER)
        self.sender = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_SENDER)
        self.receiver = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_RECEIVER)
        self.username = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_USERNAME)
        self.password = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_PASSWORD)

        """
        用例配置
        """
        self.case_id=int(self.get_conf(Config.TITLE_CASE, Config.VALUE_CASEID))
        self.case_name=int(self.get_conf(Config.TITLE_CASE, Config.VALUE_CASENAME))
        self.case_rank=int(self.get_conf(Config.TITLE_CASE, Config.VALUE_CASERANK))
        self.case_desc=int(self.get_conf(Config.TITLE_CASE, Config.VALUE_CASEDESC))
        self.case_data=int(self.get_conf(Config.TITLE_CASE, Config.VALUE_CASEDATA))
        self.case_expect=int(self.get_conf(Config.TITLE_CASE, Config.VALUE_CASEEXPECT))

    def get_conf(self, title, value):
            """
            配置文件读取
            :param title:
            :param value:
            :return:
            """
            return self.config.get(title, value)

    def set_conf(self, title, value, text):
        """
        配置文件修改
        :param title:
        :param value:
        :param text:
        :return:
        """
        self.config.set(title, value, text)
        with open(self.conf_path, "w+") as f:
            return self.config.write(f)

    def add_conf(self, title):
        """
        配置文件添加
        :param title:
        :return:
        """
        self.config.add_section(title)
        with open(self.conf_path, "w+") as f:
            return self.config.write(f)

if __name__ == '__main__':
    import requests
    import json
    url=Config().loginHost_test
    data=eval(Config().loginData_test)
    head={
        "Content-Type":"application/json"
    }
    data=json.dumps(data)
    res=requests.post(url=url,headers=head,data=data).json()
    print(res["data"]["token"])
